import Axios from "axios";
import envconfig from "./env";
import { Toast } from "vant";
import "vant/es/toast/style";
const whiteList = ["/myCourse/studyCourseList","/collect"];
const http = Axios.create({
  //   baseURL: 'http://www.ysqorz.top:8888/api/private/v1',
  baseURL: envconfig.prod.baseURL,
  timeout: 5000,
});
http.interceptors.request.use(
  (config) => {
    Toast.loading({
      message: "加载中...",
      forbidClick: true,
      loadingType: "spinner",
      duration: 0,
    });
    console.log(config);
    //配置请求头,或自定义传给后台头信息 通常传token
    if (whiteList.includes(config.url)) {
      const token = localStorage.getItem("token");
      token && (config.headers.zywxtoken = "zywx" + token);
    }

    //返回config
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  (res) => {
    const successArr = [200];

    if (successArr.includes(res.data.code)) {
      Toast.clear();
      return res;
    }

    //返回报错提示
    Toast.loading({
      message: res.data.msg,
      forbidClick: true,
    });
    return Promise.reject(res.data.msg);
  },
  (error) => {
    return Promise.reject(error);
  }
);
export default http;
